#include "formqdserverinfo.h"
#include "ui_formqdserverinfo.h"
#include "sysinfo.h"

#include <QDebug>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>

FormDQServerInfo::FormDQServerInfo(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FormQDServerInfo)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() | Qt::Dialog);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_DeleteOnClose);

    mqtt.mqttConnect();
    mqtt.subscribe("inner");

    connect(&timer, &QTimer::timeout, this, &FormDQServerInfo::on_tick);
    timer.setInterval(3000);
    timer.setSingleShot(false);
    timer.start();

    connect(&mqtt, &MQTT_IPC::recvMsg, this, &FormDQServerInfo::on_recvMQTTMsg);

    qDebug() << "[FormDQServerInfo] \t Constructor";
}

FormDQServerInfo::~FormDQServerInfo()
{
    mqtt.mqttDisconnect();
    delete ui;

    qDebug() << "[FormDQServerInfo] \t Destructor";
}

void FormDQServerInfo::on_pushButtonReturn_clicked()
{
    this->close();
}

void FormDQServerInfo::on_tick()
{
    Sysinfo sysinfo;
    QString ret;

    //发送强电连接信息查询
    mqtt.sendMsg("inner", "{ \
                 \"src\": \"dcsmini\", \
                 \"dst\": [\"excqdapp\"], \
                 \"payload\": { \
                     \"cmd\": \"qd-evt-query\", \
                     \"sn\": 123 \
                 }}");


    //获取并显示强电ip和端口信息
    ret = sysinfo.getQdSrvInfo();
    if (ret.size() > 0)
    {
        ui->labelServerIP->setText(ret.section(',', 0, 0).trimmed());
        ui->labelServerPort->setText(ret.section(',', 1, 1).trimmed());
    }
    else
    {
        ui->labelServerIP->setText("");
        ui->labelServerPort->setText("");
    }
}

void FormDQServerInfo::on_recvMQTTMsg(QByteArray msg)
{
    QJsonParseError err_rpt{};
    QJsonDocument root_Doc = QJsonDocument::fromJson(msg, &err_rpt); // 字符串格式化为JSON

    if (err_rpt.error != QJsonParseError::NoError)
    {
        qDebug() << "JSON格式错误";
    }
    else    // JSON格式正确
    {
        QString srcapp, dstapp;
        QJsonValue srcValue, dstValue, payloadValue;
        QJsonObject root_Obj = root_Doc.object();

        srcValue = root_Obj.value("src");
        if (srcValue.isString())
        {
            srcapp = srcValue.toString();
        }

        dstValue = root_Obj.value("dst");
        if (dstValue.isArray())
        {
            dstapp = dstValue.toArray().at(0).toString();
        }

        if (srcapp == "excqdapp" && dstapp == "dcsmini")
        {
            payloadValue = root_Obj.value("payload");
            if (payloadValue.isObject())
            {
                QString cmdStr = payloadValue.toObject().value("cmd").toString();
                QString retStr = payloadValue.toObject().value("result").toString();
                if (cmdStr == "qd-evt-query" && retStr == "success")
                {
                    QJsonArray evtValue = payloadValue.toObject().value("evt").toArray();
                    for (int i = 0; i < evtValue.size(); i++)
                    {
                        QJsonObject item = evtValue.at(i).toObject();
                        qint16 evtId = item.value("evt-id").toInt();
                        QString evtCtx = item.value("evt-ctx").toString();
                        if (evtId == 10001)
                        {
                            if (evtCtx == "YES")
                            {
                                ui->labelQDServerLinkState->setText(tr("已连接"));
                            }
                            else
                            {
                                ui->labelQDServerLinkState->setText(tr("未连接"));
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
}
